package Dao;

import DataClass.Graduate;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class GraduateDao {

    private static PreparedStatement preparedStatement = null;
    public static List<Graduate> getGraduate() throws SQLException {
        List<Graduate> list = new ArrayList<>();

        ResultSet data = GraduateDao.getDataGraduate();

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        while (data.next()) {
            String SNo = data.getString("SNo");
            String SSex = data.getString("SSex");
            String SType = data.getString("SType");
            // 获取并格式化日期
            String SIn = data.getTimestamp("SIn").toLocalDateTime().toLocalDate().format(formatter);
            String SOut = data.getTimestamp("SOut").toLocalDateTime().toLocalDate().format(formatter);
            list.add(new Graduate(SNo, SSex, SType, SIn, SOut, ""));
        }
        return list;
    }

    public static ResultSet getDataGraduate() throws SQLException {
        if(preparedStatement == null)
        {
            String sql = "select * from Student where SOut is NOT NULL";
            preparedStatement = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet data = preparedStatement.executeQuery();
        JBDCUtil.commit();

        return data;
    }
}
